package net.streaming.rtsp.server;

import java.net.URI;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


public class RtspRequest{
	private String cmd;
	private URI uri;
	private String protocol;
	private Map<String,String> headers = new LinkedHashMap<String,String>();
	private List<String> _lineList = new ArrayList<String>(); 
	
	public RtspRequest() {
	}
	
	public static RtspRequest parse(List<String> list){
		RtspRequest request = new RtspRequest();
		request._lineList = list;
		String firstLine = list.get(0);
		String[] sArr = firstLine.split(" ");
		request.cmd = sArr[0];
		try {
			request.uri = new URI(sArr[1]);
		} catch (Exception e) {
			e.printStackTrace();
		}
		request.protocol = sArr[2];
		for(int i=1;i<list.size();i++){
			String line = list.get(i);
			String[] arr = line.split(": ");
			request.headers.put(arr[0], arr[1]);
		}
		return request;
	}
	
	public String getCmd() {
		return cmd;
	}
	public void setCmd(String cmd) {
		this.cmd = cmd;
	}
	public URI getUri() {
		return uri;
	}
	public void setUri(URI url) {
		this.uri = url;
	}
	public String getProtocol() {
		return protocol;
	}
	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}
	
	public Map<String, String> getHeaders() {
		return headers;
	}

	public void setHeaders(Map<String, String> headers) {
		this.headers = headers;
	}
	
	public String getHeader(String name){
		if(this.headers==null){
			return null;
		}
		return this.headers.get(name);
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for(String line:_lineList){
			sb.append(line);
			sb.append("\r\n");
		}
		return sb.toString();
	}
}